package com.ricchieri.reports;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.model.Charter;
import com.ricchieri.model.CharterExpense;
import com.ricchieri.util.FacesUtil;

public class CharterReport extends ReportTemplate {
	private static final long serialVersionUID = 3745017294788565719L;
	private Charter charter;

	@Override
	protected void addParameters(Map<String, Object> parameters) {
		parameters.put(BusinessSettings.REPORT_CHARTER_NUMBER, charter.getCharterNumber());
		parameters.put(BusinessSettings.REPORT_CHARTER_PATENT, charter.getPatent());
		parameters.put(BusinessSettings.REPORT_CHARTER_KILOMETERS, charter.getKilometers());
		parameters.put(BusinessSettings.REPORT_CHARTER_TYPE, FacesUtil.getMessage(charter.getCharterType().getDescription()));
		parameters.put(BusinessSettings.REPORT_CHARTER_AVAILABILITY, charter.getPlaces());	
		
		parameters.put(BusinessSettings.REPORT_CHARTER_OBSERVATIONS, charter.getObservation());
		parameters.put(BusinessSettings.REPORT_CHARTER_DATE, charter.getStart());
	}

	@Override
	public void generateReport(Map<String, Object> map,
			ReportFileFormat reportFileFormat) {
		 
		initReport(BusinessSettings.CHARTER_REPORT, reportFileFormat);
		
		charter = (Charter) map.get(BusinessSettings.KEY_CHARTER);
		
		try {
			List <CharterExpense> charterExpenses = charter.getCharterExpenses();
			
			if(charterExpenses == null) {
				charterExpenses = new ArrayList<CharterExpense>();
			}
			
			String result = doWork(charterExpenses);
			
			if(!BusinessSettings.SUCCESS.equals(result)){
				FacesUtil.showInfoMessage("exportReport", "reportError");
			} else {
				FacesUtil.showInfoMessage("exportReport", "reportSuccess");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			FacesUtil.showInfoMessage("exportReport", "reportError");
		}

	}

}
